package com.jeeplus.modules.sys.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.json.ReturnJson;
import com.jeeplus.common.utils.FileUtils;
import com.jeeplus.common.utils.ResponseCode;
import com.jeeplus.common.utils.TreeDataMake;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sys.entity.DataItemDetailEntity;
import com.jeeplus.modules.sys.entity.DataItemEntity;
import com.jeeplus.modules.sys.entity.TreeModel;
import com.jeeplus.modules.sys.service.DataItemService;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @title: DataItemController
 * @projectName JeePlus
 * @description: TODO
 * @date 2019/10/2911:51
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/DataItem")
public class DataItemController extends BaseController {
    @Autowired
    DataItemService apiService;
    private ResponseCode responseCode;

    @RequestMapping(value = "index")
    public String index(HttpServletRequest request,HttpServletResponse response){
        return "modules/sys/DataItem/index";
    }
    @RequestMapping(value = "Form")
    public String Form(HttpServletRequest request,HttpServletResponse response){
        return "modules/sys/DataItem/Form";
    }
    @RequestMapping(value = "ClassifyIndex")
    public String ClassifyIndex(HttpServletRequest request,HttpServletResponse response){
        return "modules/sys/DataItem/ClassifyIndex";
    }
    @RequestMapping(value = "ClassifyForm")
    public String ClassifyForm(){
        return "modules/sys/DataItem/ClassifyForm";
    }
    @RequestMapping(value = "GetTree", method = RequestMethod.GET)
    @ResponseBody
    public String GetTree(HttpServletRequest request, HttpServletResponse response)  {
        try {
            return FileUtils.getFileContent("d:/json.json");
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    @RequestMapping(value = "GetClassifyTree", method = RequestMethod.GET)
    @ResponseBody
    public ReturnJson GetClassifyTree(HttpServletRequest request, HttpServletResponse response){
        ReturnJson ajaxJson=new ReturnJson();
        List<TreeModel> treeList=apiService.GetClassifyList();
        ajaxJson.setCode(200);
        ajaxJson.setInfo("响应成功");
        ajaxJson.setData(new TreeDataMake().ToTree(treeList,""));
        return ajaxJson;
    }
    @RequestMapping(value = "GetDetailList", method = RequestMethod.GET)
    @ResponseBody
    public ReturnJson GetDetailList(HttpServletRequest request,HttpServletResponse response,String itemCode){
        ReturnJson ajaxJson=new ReturnJson();
        List<DataItemDetailEntity> treeList=apiService.GetDetailList(itemCode);
        ajaxJson.setCode(200);
        ajaxJson.setInfo("响应成功");
        ajaxJson.setData(treeList);
        return ajaxJson;
    }
    @RequestMapping(value = "ExistDetailItemName", method = RequestMethod.GET)
    @ResponseBody
    public ReturnJson ExistDetailItemName(String keyValue, String F_ItemName, String itemCode){
        ReturnJson ajaxJson=new ReturnJson();
        List<DataItemDetailEntity> treeList=apiService.GetDetailList(itemCode);
        boolean res = apiService.ExistDetailItemName(keyValue,F_ItemName,itemCode);
        ajaxJson.setCode(200);
        ajaxJson.setInfo("响应成功");
        ajaxJson.setData(res);
        return ajaxJson;
    }
    @RequestMapping(value = "SaveDetailForm", method = RequestMethod.POST)
    @ResponseBody
    public ReturnJson SaveDetailForm(String keyValue, String itemCode,DataItemDetailEntity entity){
        ReturnJson ajaxJson=new ReturnJson();
        boolean res=apiService.SaveDetailEntity(keyValue,itemCode,entity);
        //System.out.println(res);
        ajaxJson.setCode(200);
        ajaxJson.setInfo("响应成功");
        ajaxJson.setData(res);
        return ajaxJson;
    }
    @RequestMapping(value = "DeleteDetailForm", method = RequestMethod.POST)
    @ResponseBody
    public ReturnJson DeleteDetailForm(String keyValue){
        ReturnJson ajaxJson=new ReturnJson();
        boolean res=apiService.DeleteDetailForm(keyValue);
        ajaxJson.setCode(200);
        ajaxJson.setInfo("响应成功");
        ajaxJson.setData(res);
        return ajaxJson;
    }
    @RequestMapping(value = "GetClassifyList",method = RequestMethod.GET)
    @ResponseBody
    public ReturnJson GetClassifyList(String keyword){
        ReturnJson ajaxJson=new ReturnJson();
        List<DataItemEntity> list= apiService.GetDataItemEntityList();
        List<TreeModel> l=new ArrayList<>();
//        System.out.println(keyword);
//        System.out.println(JsonMapper.toJsonString(list));
        ajaxJson.setCode(200);
        ajaxJson.setInfo("响应成功");
        ajaxJson.setData(list);
        return ajaxJson;
    }
    @RequestMapping(value = "SaveClassifyForm")
    @ResponseBody
    public ReturnJson SaveClassifyForm(String keyValue, DataItemEntity entity){
        ReturnJson ajaxJson=new ReturnJson();
        boolean res=apiService.SaveClassifyForm(keyValue,entity);
        ajaxJson.setCode(200);
        ajaxJson.setInfo("响应成功");
        ajaxJson.setData(res);
        return ajaxJson;
    }
    @RequestMapping(value = "DeleteClassifyForm")
    @ResponseBody
    public ReturnJson DeleteClassifyForm(String keyValue){
        ReturnJson ajaxJson=new ReturnJson();
        boolean res=apiService.DeleteClassifyForm(keyValue);
        ajaxJson.setCode(200);
        ajaxJson.setInfo("响应成功");
        ajaxJson.setData(res);
        return ajaxJson;
    }
    @RequestMapping(value = "ExistItemCode")
    @ResponseBody
    public ReturnJson ExistItemCode(HttpServletRequest request,String keyValue, String F_ItemCode){
        ReturnJson ajaxJson=new ReturnJson();
        boolean res=apiService.ExistItemCode(keyValue,F_ItemCode);
        ajaxJson.setCode(200);
        ajaxJson.setInfo("响应成功");
        ajaxJson.setData(res);
        return ajaxJson;
    }
    @RequestMapping(value = "ExistItemName")
    @ResponseBody
    public ReturnJson ExistItemName(String keyValue, String F_ItemName){
        ReturnJson ajaxJson=new ReturnJson();
        boolean res=apiService.ExistItemName(keyValue,F_ItemName);
        ajaxJson.setCode(200);
        ajaxJson.setInfo("响应成功");
        ajaxJson.setData(res);
        return ajaxJson;
    }
    @RequestMapping(value = "ExistDetailItemValue")
    @ResponseBody
    public ReturnJson ExistDetailItemValue(String keyValue, String F_ItemValue, String itemCode){
        ReturnJson ajaxJson=new ReturnJson();
        boolean res=apiService.ExistDetailItemValue(keyValue,F_ItemValue,itemCode);
        ajaxJson.setCode(200);
        ajaxJson.setInfo("响应成功");
        ajaxJson.setData(res);
        return ajaxJson;
    }
    @RequestMapping(value = "GetMap")
    @ResponseBody
    public ReturnJson GetMap(String code){
       // System.out.println(code);
        ReturnJson ajaxJson=new ReturnJson();
        ajaxJson.setCode(200);
        ajaxJson.setInfo("响应成功");
        ajaxJson.setData(apiService.GetMap(code));
        return ajaxJson;
    }
    @RequestMapping(value = "treeData")
    @ResponseBody
    public List<Map<String, Object>> treeData(String keyValue) throws IOException {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<Map<String,Object>> list=apiService.treeData(keyValue);
        for (int i=0;i<list.size();i++){
            Map<String,Object> e=list.get(i);
            Map<String, Object> map = Maps.newHashMap();
            map.put("id", e.get("F_ITEMID"));
            if("0".equals(e.get("F_PARENTID"))){
                map.put("parent", "#");
                Map<String, Object> state = Maps.newHashMap();
                state.put("opened", true);
                map.put("state", state);
            }else{
                map.put("parent",e.get("F_PARENTID"));
            }
            map.put("name", e.get("F_ITEMCODE"));
            map.put("text", e.get("F_ITEMNAME"));
            mapList.add(map);
        }

        return mapList;
    }
}
